package com.ckm.controller;

import com.ckm.annotation.LogInfo;
import com.ckm.entity.ResponseResult;
import com.ckm.service.StudentService;
import com.ckm.vo.LoginUserVo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@LogInfo
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 登录
     * @param loginUserVo 登录信息
     * @return 登录成功对象
     */
    @PostMapping("login")
    public ResponseResult login(@RequestBody LoginUserVo loginUserVo) {
        //参数校验，在控制层还是服务层
        Assert.notNull(loginUserVo,"登录信息不能为空");
        Assert.notNull(loginUserVo.getPassword(),"密码不能为空");
        Assert.notNull(loginUserVo.getRePassword(),"再次输入密码不能为空");
        return new ResponseResult(200,"登录成功",studentService.login(loginUserVo));
    }

    /**
     * 通过请求头中的token获取用户登录信息
     * @param request 请求
     * @return 登录成功的用户信息
     */
    @GetMapping("/info")
    public ResponseResult getStudent(HttpServletRequest request){
        return new ResponseResult(200,"获取用户信息",studentService.getStudent(request.getHeader("token")));
    }
}
